<template>
  <div class="dialog">
    <el-dialog :title="'作业记录' + titleText" :visible.sync="showDialog" width="45%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="鱼塘名称:" prop="fishPondId" class="w50p">
          <el-select filterable clearable v-model="formData.fishPondId" @change="queryBatch">
            <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
          </el-select>
        </el-form-item>
        <el-form-item label="批次编号:" prop="productionBatchId" class="w50p">
          <el-select filterable clearable v-model="formData.productionBatchId">
            <el-option v-for="(item, index) in batchList" :key="index"
              :label="`${item.mnecode}_${item.varietyName}`" :value="item.productionBatchId" />
          </el-select>
        </el-form-item>
        <el-form-item label="消毒药品 ：" class="w100p">
          <div v-if="nowChanceStau === 'add'" class="line-click" @click="addMaterial()">
            <i class="el-icon-plus"></i>
          </div>
          <div class="hang-content">
            <div v-for="(key, index) in BOList" :key="index + 'deviceCode'" class="hang-content-one">
              <el-select filterable clearable v-model="key.materialId"  placeholder="物料选择">
                <el-option v-for="(item, indexk) of materialList" :key="item.materialId + 'material' + indexk"
                  :label="item.name" :value="item.materialId" />
              </el-select>
              <el-input v-model="key.num" clearable placeholder="请填写数量" />
              <div v-if="nowChanceStau === 'add'" class="line-click" @click="delMaterial(index)">
                <i class="el-icon-minus"></i>
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="作业人:" prop="staffId" class="w50p">
          <el-select filterable clearable v-model="formData.staffId" value-key="staffId" @change="staffChange">
            <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName" :value="item.staffId" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业时间：" class="w50p" prop="workDateTime">
          <el-date-picker v-model="formData.workDateTime" type="datetime" placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import DisinfectionOperationApi from "@/api/operation-management/disinfection-operation"
import BatchMangeApi from "@/api/batch-mange"
import { dialogMixin } from "@/utils/dialogMixin";
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
    staffList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    pondList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    materialList: {
      type: [Array],
      default: () => {
        return []
      }
    },
  },
  data () {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      rules: {
        storeHouseName: [
          { required: true, message: "请输入批次编号", trigger: ["change", "blur"] }
        ],
        area: [
          { required: true, message: "请输入批次面积", trigger: ["change", "blur"] }, {
            validator: checkInt, trigger: ["change", "blur"]
          }
        ],
      },
      images: [],
      BOList: [{ num: "", materialId: "", price: "" }],
      fileIdList: [],
      formData: {},
      batchList: [],//批次
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
          workDateTime: this.getNowTime()
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
        this.BOList = this.editObject.materialList
      }
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    queryBatch (e) {
      BatchMangeApi.queryBatchByPondId({ pondId: e }).then(res => {
        this.batchList = res.data
      })
    },
    staffChange (e) {
      this.staffList.find((item) => {
        if (item.staffId === e) {
          this.formData.staffName = item.staffName
          return true
        }
      })
    },
    // 点击添加按按钮
    addMaterial () {
      let list = {};
      this.$set(this.BOList, this.BOList.length, list);
      this.BOList = [...this.BOList];
    },
    // 删除改行
    delMaterial (index) {
      this.BOList.splice(index, 1);
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            this.formData.materialList = this.BOList
            DisinfectionOperationApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            this.formData.materialList = this.BOList
            DisinfectionOperationApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
.line-click {
  min-width: 30px;
  height: 30px;
  // background-color: aqua;
  display: flex;
  justify-content: center;
  align-items: center;
  border: snow 1px solid;
  border-radius: 50%;
  margin-bottom: 10px;
  margin-top: 5px;
  margin-left: 5px;
  color: #fff;
  background-color: #003947;
  cursor: pointer;
}

.hang-content {
  width: 100%;

  .hang-content-one {
    width: 100%;
    display: flex;
  }
}
</style>
